package org.anyline.simple.mongodb;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerApi;
import com.mongodb.ServerApiVersion;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.anyline.data.datasource.DataSourceHolder;
import org.anyline.data.mongodb.entity.MongoDataRow;
import org.anyline.data.param.ConfigStore;
import org.anyline.data.param.init.DefaultConfigStore;
import org.anyline.data.runtime.RuntimeHolder;
import org.anyline.entity.Compare;
import org.anyline.entity.DataSet;
import org.anyline.entity.DefaultPageNavi;
import org.anyline.entity.PageNavi;
import org.anyline.metadata.Table;
import org.anyline.proxy.ServiceProxy;
import org.anyline.service.AnylineService;
import org.anyline.util.ConfigTable;
import org.bson.conversions.Bson;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.DatabaseMetaData;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@SpringBootTest
public class MongoTest {
    @Test
    public void ds() throws Exception {

        String url = "mongodb://localhost:27017";
        Map<String, Object> params = new HashMap<>();
        params.put("url", url);
        params.put("database", "hr");

        DataSourceHolder.reg("hr2", params);

        DataSet set = new DataSet();
        long fr = System.currentTimeMillis();
        for(int i=0; i<10;i ++){
            MongoDataRow row = new MongoDataRow();
            row.put("_id", fr+i);
            row.put("name", "USER_"+i);
            row.put("code"+i, "CODE"+i);
            row.put("age",i);
            set.add(row);
        }ServiceProxy.service("hr2").truncate("crm_user6");
        ServiceProxy.service("hr2").insert("crm_user6", set);
        set = ServiceProxy.service("hr2").querys("crm_user6");

        System.out.println(set);
        LinkedHashMap<String, Table> tables = ServiceProxy.service("hr2").metadata().tables();
        System.out.println(tables);

    }
    @Test
    public void init(){
        String table =  "crm_user6";
        ConfigTable.PRIMARY_GENERATOR_SNOWFLAKE_ACTIVE = true;
        AnylineService service = ServiceProxy.service("hr");
        DataSet set = new DataSet();
        for(int i=0; i<10;i ++){
            MongoDataRow row = new MongoDataRow();
            //row.put("_id", fr+i);
            row.put("name", "USER_"+i);
            row.put("code", "CODE"+i);
            row.put("age",i);
            set.add(row);
        }
        MongoDataRow row = new MongoDataRow();
        //row.put("_id", fr+i);
        row.put("name", "USER_"+1);
        row.put("code", "CODE"+1);
        row.put("age",1);
        ConfigTable.DEFAULT_MONGO_ENTITY_CLASS = MongoDataRow.class;
        //insert时 _id不可以重复
        //插入多行
        service.insert(table, set);
        Assertions.assertEquals(10, set.size());
        row = (MongoDataRow) service.query(table);

        if(null == row){
            row = new MongoDataRow();
        }
        //更新一行 根据_id
        row.put("name", "test");
        service.save(table, row);

        row = (MongoDataRow) service.query(table ,"name:test");
        System.out.println("修改结果:"+row);

        //根据条件更新
        service.update(table, row, new DefaultConfigStore().and(Compare.GREAT, "age", 1));
        System.out.println(service.querys(table));
        service.delete(row);

        service.delete(table, "_id", "1");

        service.deletes(table, "code", "CODE1","CODE2");

        //查询整个表
        set = service.querys(table);
        //查部分列 主键_id默认查询
        set = service.querys("crm_user2(age,code)");
        System.out.println("查询结果:"+set.size());
        System.out.println("查询结果结构:"+set.getRow(0));
        //不查AGE和_id
        set = service.querys("crm_user2(!age,!_id)");

        System.out.println("查询结果:"+set.size());
        if(set.size()>0) {
            System.out.println("查询结果结构:" + set.getRow(set.size() - 1));
        }
        //按条件查询
        set = service.querys(table,
                new DefaultConfigStore().and(Compare.GREAT,"age", 1)
                        .and(Compare.LESS_EQUAL, "age", 5)
                        .in("age", new Integer[]{1,2})
                ,"name:%1%","code:%1");

        System.out.println("查询结果:"+set.size());
        if(!set.isEmpty()) {
            System.out.println("查询结果结构:" + set.getRow(set.size() - 1));
        }
        //分页
        set = service.querys(table, new DefaultPageNavi(2).setPageRows(2));
        PageNavi navi = set.getNavi();
        System.out.println("查询结果: 总行数="+navi.getTotalRow()+",总页数:"+navi.getTotalPage()+",当前页:"+navi.getCurPage()+",结果集:"+set);

        ConfigStore delete = new DefaultConfigStore();
        delete.and("age", 1);
        service.delete(table, delete);
    }
}
